package com.explorati.interview.interview.chaptersix;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

public class RanAccFileTest {

	public static void main(String[] args) {
		RandomAccessFile file = null;
		try {
			file = new RandomAccessFile("e:/test/a.txt", "rw");
			for (int i = 0; i < file.length(); i++) {
				// read()方法读取一个字节
				byte b = (byte) file.read();
				// 转换为char型
				char c = (char) b;
				if (c == 'a') {
					// 将指针恢复到之前的地方
					file.seek(i);
					// 写入新的字母c
					file.write('c');
				}
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				file.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}
}
